package com.guimin.wuye.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guimin.site.entity.GuiminSite;
import com.guimin.user.api.UserInfoApi;
import com.guimin.user.dto.LoginInfoDto;
import com.guimin.wuye.dto.AddPropertyDto;
import com.guimin.wuye.entity.GmProperty;
import com.guimin.wuye.service.GmPropertyService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 物业表(GuiminWuye)控制层
 *
 * @author makejava
 * @since 2021-11-24 11:25:19
 */
@RestController
@RequestMapping("/property")
public class GuiminPropertyApi {
    /**
     * 服务对象
     */
    @Resource
    private GmPropertyService gmPropertyService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private UserInfoApi userInfoApi;


    /**
     * 获取物业列表
     * @return
     */
    @GetMapping("all")
    public R<List<GmProperty>> all() {
        List<GmProperty> list = gmPropertyService.list();
        return R.ok(list);
    }
    /**
     * 获取物业列表
     * @param request
     * @param current
     * @param size
     * @return
     */
    @GetMapping("list")
    public R<IPage<GmProperty>> list(HttpServletRequest request,
                                     @RequestParam(defaultValue = "1") Integer current,
                                     @RequestParam(defaultValue = "10") Integer size) {
        LoginInfoDto login = userInfoApi.getLogin(request);
        IPage<GmProperty> page = gmPropertyService.page(new Page<>(current, size));
        return R.ok(page);
    }

    /**
     * 添加物业
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody @Validated AddPropertyDto dto) {
        boolean save = gmPropertyService.addProperty(dto);
        return save ? R.ok(null) : R.failed("添加失败");
    }

    /**
     * 编辑物业
     * @param dto
     * @return
     */
    @PutMapping("/edit")
    public R<String> edit(@RequestBody GmProperty dto) {
        if (dto.getId() == null) {
            return R.failed("id不能为空");
        }
        boolean update = gmPropertyService.updateById(new GmProperty()
                .setId(dto.getId())
                .setName(dto.getName())
                .setAdminName(dto.getAdminName())
                .setPhone(dto.getPhone())
                .setUpdateDatetime(new Date()));
        return update ? R.ok(null) : R.failed("修改失败");
    }

    /**
     * 删除物业
     * @param wuyeId
     * @return
     */
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam Integer wuyeId) {
        boolean remove = gmPropertyService.update(new GmProperty().setState("已删除"),new LambdaQueryWrapper<GmProperty>().eq(GmProperty::getId,wuyeId));
        return remove ? R.ok(null) : R.failed("删除失败");
    }


}

